package com.qianya.oa.interceptor;

import com.qianya.oa.common.utils.uuid.IdUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Component
@Aspect
@Slf4j
public class ControllerAspect {

    @Pointcut("!execution(* com.qianya.oa.controller..*Controller.init(..)) && execution(* com.qianya.oa.controller..*Controller.*(..))")
    public void logPointcut() {
    }

    @Around("logPointcut()")
    public Object log(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        String id = IdUtils.fastSimpleUUID();
        long first = System.currentTimeMillis();

        String className = proceedingJoinPoint.getTarget().getClass().getSimpleName();
        String methodName = proceedingJoinPoint.getSignature().getName();
        Object[] args = proceedingJoinPoint.getArgs();
        String classMethodName = className + "." + methodName + "()";
        log.info("日志标识:{},类方法:{},参数:{}", id, classMethodName, args);
        Object res = proceedingJoinPoint.proceed();
        long duration = System.currentTimeMillis() - first;
        log.info("日志标识:{},类方法:{},耗时:{}ms", id, classMethodName, duration);
        return res;
    }

}
